<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="充值账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入充值账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品拓展" prop="goodsExpandId">
        <el-input
          v-model="queryParams.goodsExpandId"
          placeholder="请输入商品拓展主键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="拓展商品类型" prop="goodsExpandType">
        <el-input
          v-model="queryParams.goodsExpandType"
          placeholder="请输入拓展商品类型"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vip:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增主键" align="center" prop="id" />
      <el-table-column label="订单编号" align="center" prop="orderNum" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="充值账号" align="center" prop="account" />
      <el-table-column label="联系方式" align="center" prop="contact" />
      <el-table-column label="咨询价格" align="center" prop="consultingPrice" />
      <el-table-column label="最终价格" align="center" prop="price" />
      <el-table-column label="商品主键" align="center" prop="goodsId" />
      <el-table-column label="商品拓展主键" align="center" prop="goodsExpandId" />
      <el-table-column label="拓展商品类型" align="center" prop="goodsExpandType" :formatter="goodsExpandTypeFormat" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status!=1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="pricing(scope.row)"
            v-hasPermi="['vip:order:edit']"
          >定价</el-button>

          <el-button
            :disabled="scope.row.status!=4"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="confirmThePayment(scope.row)"
            v-hasPermi="['vip:order:edit']"
          >确认付款</el-button>

          <el-button
            :disabled="!(scope.row.status!=4 || scope.row.status!=5)"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="sendCode(scope.row)"
            v-hasPermi="['vip:order:edit']"
          >验证码</el-button>

          <el-button
            :disabled="scope.row.status!=6"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="deliverGoods(scope.row)"
            v-hasPermi="['vip:order:edit']"
          >发货</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 定价编辑框 -->
    <el-dialog :title="title" :visible.sync="pricingOpen" width="500px" append-to-body>
      <el-form ref="form" :model="pricingForm" :rules="rules" label-width="80px">
        <el-form-item label="价格" prop="price">
          <el-input v-model="pricingForm.price" type="number" placeholder="请输入最终价格" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPricing">确 定</el-button>
        <el-button @click="cancelPricing">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
    import {
        addOrder,
        delOrder,
        getOrder,
        listOrder,
        updateOrder,
        pricing,
        sendCode,
        confirmThePayment,
        deliverGoods
    } from "@/api/vip/order";
    import ImageUpload from '@/components/ImageUpload';
    import FileUpload from '@/components/FileUpload';
    import Editor from '@/components/Editor';

export default {
  name: "Order",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示定价弹出层
      pricingOpen: false,
      // 状态字典
      statusOptions: [],
      // 拓展商品类型字典
      goodsExpandTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        account: null,
        contact: null,
        goodsId: null,
        goodsExpandId: null,
        goodsExpandType: null,
      },
      // 定价表单参数
      pricingForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("order_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("goods_expand_type").then(response => {
      this.goodsExpandTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 拓展商品类型字典翻译
    goodsExpandTypeFormat(row, column) {
      return this.selectDictLabel(this.goodsExpandTypeOptions, row.goodsExpandType);
    },
    // 取消定价按钮
    cancelPricing() {
      this.pricingOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.pricingForm = {
        id: null,
        orderNum: null,
        status: 0,
        account: null,
        contact: null,
        consultingPrice: null,
        price: null,
        goodsId: null,
        goodsExpandId: null,
        goodsExpandType: null,
        remark: null,
        crateTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    pricing(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.pricingForm = response.data;
        this.pricingOpen = true;
        this.title = "定价";
      });
    },
    /** 提交定价按钮 */
    submitPricing() {
          if (this.pricingForm.id != null) {
              pricing(this.pricingForm).then(response => {
              this.msgSuccess("定价成功");
              this.pricingOpen = false;
              this.getList();
            });
          }
    },
    /**通知验证码*/
    sendCode(row){
      const ids = row.id;
      this.$confirm('确认通知用户接收验证码?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return sendCode(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("已发送通知");
        })
    },
    /**确认付款*/
    confirmThePayment(row){
      const ids = row.id
      this.$confirm('确认用户已经付款?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return confirmThePayment(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("确认成功");
        })
    },
    /**确认发货*/
    deliverGoods(row){
      const ids = row.id;
      this.$confirm('确认已发货?', "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deliverGoods(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("确认成功");
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vip/order/export', {
        ...this.queryParams
      }, `vip_order.xlsx`)
    }
  }
};
</script>
